#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QPainter"
#include <QTimer>
#include <QMovie>
#include <QPropertyAnimation>
#include <QRect>
#include <QEasingCurve>
#include <QDebug>
#include <QDialog>
#include "audioplayer.h"
#include "message1.h"
#include "option.h"
#include "game.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);

    this->setCursor(QCursor(QPixmap(":/img/mouse"),0,0));

    //文件读取
    {
    QFile readFile("inf.dat");
    if(!readFile.open(QIODevice::ReadOnly))
    {
        scene_number=1;
        qDebug()<<"新游戏";
        ui->toolButton->setVisible(false);
    }
    else
    {
        qDebug()<<"读入数据";
        QDataStream in(&readFile);
        in>>*this;
        readFile.close();
    }
    }

    //音频初始化
    {
       // audio= new AudioPlayer(this);
       // audio->startBGM();
    }

    //设置栏
    option = new Option(this);
    nowRect.setRect(0,0,1280,720);
    ui->game->mainwindow=this;
    //game=ui->game;


    //测试
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(updateMap()));
    timer->start(30);
    //p=new QPainter;
    i=0;
    //
}

void MainWindow::updateMap()
{
    i++;
    //qDebug()<<i;
    update();
}

void MainWindow::changeWindow(int id)
{
    ui->stackedWidget->setCurrentIndex(id);
}
MainWindow::~MainWindow()
{
    delete ui;
    //文件写出
    {
    QFile writeFile("inf.dat");
    if(!writeFile.open(QIODevice::WriteOnly))qDebug()<<"写文件失败";
    else{ qDebug()<<"写入数据";
    QDataStream out(&writeFile);
    out<<*this;
    writeFile.close();
    }
    }


}

void MainWindow::paintEvent(QPaintEvent *)
{
    //p->drawText(QRect(100,100,100,100),QString::number(i));
}

QDataStream& operator <<(QDataStream &out, const MainWindow &u)    //写文件
{
    qDebug()<<"outData";
    out<<u.scene_number;
    return out;
}

QDataStream& operator >>(QDataStream &in, MainWindow &u)           //读文件
{
    qDebug()<<"inData";
    in>>u.scene_number;
    return in;
}




void MainWindow::on_toolButton_clicked()       //继续游戏
{
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::on_toolButton_3_clicked()     //退出游戏
{
    Message1 meg;
    meg.setmessage("是否退出游戏");
    if(meg.exec()==QDialog::Accepted)
    {
         this->close();
    }
}

void MainWindow::on_toolButton_8_toggled(bool checked)//静音控制
{
   // if(checked)audio->setMuted(true);
   // else audio->setMuted(false);
}

void MainWindow::on_toolButton_6_clicked()     //返回主菜单
{
    ui->stackedWidget->setCurrentIndex(0);
}

void MainWindow::on_toolButton_2_clicked()     //开始新游戏
{
    this->scene_number=1;
    ui->toolButton->click();
}

void MainWindow::return_MainWindow(bool clicked)           //返回主菜单接口
{
    //相关处理
    ui->stackedWidget->setCurrentIndex(0);
}

void MainWindow::on_pushButton_clicked()
{
  //暂时
    ui->game->loadscene(1);
    game=ui->game;
    qDebug()<<"gameid============="+game->id;
    ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::on_pushButton_2_clicked()
{
    qDebug()<<"yese";
}

void MainWindow::on_toolButton_pressed()
{
   // audio->playSound(0);
}

void MainWindow::on_toolButton_2_pressed()
{
   // audio->playSound(0);
}

void MainWindow::on_toolButton_3_pressed()
{
   // audio->playSound(0);
}
